package com.example.java_gobang.api;

import com.example.java_gobang.model.User;
import com.example.java_gobang.mapper.UserMapper;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@RestController
public class UserAPI {

    @Resource
    private UserMapper userMapper;

    BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();

//    @PostMapping("/login")
//    @ResponseBody
//    public Object login(String username, String password, HttpServletRequest req) {
//        // 关键操作, 就是根据 username 去数据库中进行查询.
//        // 如果能找到匹配的用户, 并且密码也一致, 就认为登录成功
//        User user = userMapper.selectByName(username);
//        System.out.println("[login] username=" + username);
//        if (user == null || !user.getPassword().equals(password)) {
//            // 登录失败
//            System.out.println("登录失败!");
//            return new User();
//        }
//        HttpSession httpSession = req.getSession(true);
//        httpSession.setAttribute("user", user);
//        return user;
//    }


    //登录更新 加上了密码加密
    @PostMapping("/login")
    @ResponseBody
    public Object login(String username, String password, HttpServletRequest request){

        // 查询用户是否在数据库中存在
        User user = userMapper.selectByName(username);

        // 没有查到
        if(user == null) {
            System.out.println("登录失败!");
            return new User();
        }else {

            //查到了,但密码不一样
            if(!bCryptPasswordEncoder.matches(password,user.getPassword())) {
                return new User();
            }
            // 匹配成功,创建 session
            request.getSession().setAttribute("user",user);
            return user;
        }
    }

//    @PostMapping("/register")
//    @ResponseBody
//    public Object register(String username, String password) {
//        try {
//            User user = new User();
//            user.setUsername(username);
//            user.setPassword(password);
//            userMapper.insert(user);
//            return user;
//        } catch (org.springframework.dao.DuplicateKeyException e) {
//            User user = new User();
//            return user;
//        }
//    }

    //注册更新，加上了密码加密
    @PostMapping("/register")
    @ResponseBody
    public Object register(String username,String password){

        User user1 = userMapper.selectByName(username);
        if(user1 != null){
            System.out.println("当前用户已存在");
            return new User();
        }else{
            User user2 = new User();
            user2.setUsername(username);
            String password1 = bCryptPasswordEncoder.encode(password);
            user2.setPassword(password1);
            userMapper.insert(user2);
            return user2;
        }
    }

    @RequestMapping("/logout")
    public void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession(false);
        try{
            session.removeAttribute("user");
            response.sendRedirect("login.html");
        }catch (NullPointerException e){
            System.out.println("session.removeAttribute()这里没有设置拦截器，直接访问logout页面退出会空指针异常");
        }


    }

    @GetMapping("/userInfo")
    @ResponseBody
    public Object getUserInfo(HttpServletRequest req) {
        try {
            HttpSession httpSession = req.getSession(false);
            User user = (User) httpSession.getAttribute("user");//拿到的是登录的用户信息
            // 拿着这个 user 对象, 去数据库中找, 找到最新的数据
            User newUser = userMapper.selectByName(user.getUsername());
            return newUser;
        } catch (NullPointerException e) {
            return new User();
        }
    }
}
